package com.wordlearning.app.controller;

import com.wordlearning.app.entity.StudyPlan;
import com.wordlearning.app.service.StudyPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/study-plans")
public class StudyPlanController {

    private final StudyPlanService studyPlanService;

    @Autowired
    public StudyPlanController(StudyPlanService studyPlanService) {
        this.studyPlanService = studyPlanService;
    }

    @PostMapping
    public ResponseEntity<StudyPlan> createStudyPlan(@RequestBody StudyPlan studyPlan) {
        try {
            StudyPlan createdStudyPlan = studyPlanService.createStudyPlan(studyPlan);
            return new ResponseEntity<>(createdStudyPlan, HttpStatus.CREATED);
        } catch (IllegalArgumentException e) {
            return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
        }
    }

    @GetMapping
    public ResponseEntity<List<StudyPlan>> getAllStudyPlans() {
        List<StudyPlan> studyPlans = studyPlanService.getAllStudyPlans();
        return new ResponseEntity<>(studyPlans, HttpStatus.OK);
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<List<StudyPlan>> getStudyPlansByUserId(@PathVariable("userId") Long userId) {
        List<StudyPlan> studyPlans = studyPlanService.getStudyPlansByUserId(userId);
        return new ResponseEntity<>(studyPlans, HttpStatus.OK);
    }

    @GetMapping("/user/{userId}/active")
    public ResponseEntity<List<StudyPlan>> getActiveStudyPlansByUserId(@PathVariable("userId") Long userId) {
        List<StudyPlan> studyPlans = studyPlanService.getActiveStudyPlansByUserId(userId);
        return new ResponseEntity<>(studyPlans, HttpStatus.OK);
    }

    @GetMapping("/{id}")
    public ResponseEntity<StudyPlan> getStudyPlanById(@PathVariable("id") Long id) {
        return studyPlanService.getStudyPlanById(id)
                .map(studyPlan -> new ResponseEntity<>(studyPlan, HttpStatus.OK))
                .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    @PutMapping("/{id}")
    public ResponseEntity<StudyPlan> updateStudyPlan(@PathVariable("id") Long id, @RequestBody StudyPlan studyPlan) {
        try {
            StudyPlan updatedStudyPlan = studyPlanService.updateStudyPlan(id, studyPlan);
            return new ResponseEntity<>(updatedStudyPlan, HttpStatus.OK);
        } catch (IllegalArgumentException e) {
            return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<HttpStatus> deleteStudyPlan(@PathVariable("id") Long id) {
        try {
            studyPlanService.deleteStudyPlan(id);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        } catch (IllegalArgumentException e) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    @PostMapping("/{studyPlanId}/complete")
    public ResponseEntity<StudyPlan> completeDailyTask(@PathVariable("studyPlanId") Long studyPlanId, 
                                                     @RequestParam("count") Integer completedCount) {
        try {
            StudyPlan updatedStudyPlan = studyPlanService.completeDailyTask(studyPlanId, completedCount);
            return new ResponseEntity<>(updatedStudyPlan, HttpStatus.OK);
        } catch (IllegalArgumentException e) {
            return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
        }
    }

    @PutMapping("/{studyPlanId}/status")
    public ResponseEntity<StudyPlan> changeStatus(@PathVariable("studyPlanId") Long studyPlanId, 
                                                @RequestParam("status") String status) {
        try {
            StudyPlan updatedStudyPlan = studyPlanService.changeStatus(studyPlanId, status);
            return new ResponseEntity<>(updatedStudyPlan, HttpStatus.OK);
        } catch (IllegalArgumentException e) {
            return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
        }
    }

}